package Frame.shopSet;

import FCRendering.FCMapAnimationConstant;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import Function.ItemUse.AItemUse;
import Function.ItemUse.FCItemUseCenter;
import Function.ItemUse.ItemUseResult;
import VWorldGameStudio.*;
import io.reactivex.schedulers.Schedulers;
import Function.FCMutilPlayCache;

import java.util.ArrayList;
import java.util.HashMap;

import static FCRendering.FCRMaster.*;
import static FCRendering.FCRMaster.gameSave;

//右键的物品的选择只能对本机的人使用
public class FunctionUseItem extends AShopFuntion {

    FixTank useObject;
    FCItem useItem;
    boolean isTankItem = false;
    private ArrayList<FixTank> serviceList = new ArrayList<>();
    FixTank witchUseItem = null;


    public FunctionUseItem() {

    }


    public void reBuildWindow() {
        witchUseItem = null;
        useObject = fcv.getSelectingUseItem();
        useItem = itemLib.getItemOfID(useObject.itemName);
        if (useItem.getCatalog().equals("tank")) {
            isTankItem = true;
        }
        serviceList.clear();
        if (isTankItem) {
            FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
            if (tank != null) {
                serviceList.add(new FixTank(tank, langLib.getNameOfId(tank.getName())));
            }
            for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
                if (!link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                    continue;
                }
                FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
                if (p != null) {
                    tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                    if (tank != null) {
                        serviceList.add(new FixTank(tank, langLib.getNameOfId(tank.getName())));
                    }
                }
            }
            //还有牵引的
            for (FCPropulsion fc : fcv.getPropulsions()) {
                if (fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")) {
                    serviceList.add(new FixTank((FCTank) fc, langLib.getNameOfId(fc.getPropulsionName())));
                }
            }
        } else {
            serviceList.add(new FixTank(player1, player1.getChName()));
            for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
                if (link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                    FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
                    serviceList.add(new FixTank(p, p.getChName()));
                }
            }
        }
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        FCRValues.leftWindowUI[0].leftText = "道具名称：" + useItem.getChName();
        FCRValues.leftWindowUI[1].leftText = "移动到";
        FCRValues.leftWindowUI[2].leftText = "丢弃";
        FCRValues.leftWindowUI[3].leftText = "对前方使用";
        FCRValues.leftWindowUI[4].leftText = "直接使用";
        for (int n = 0; n < serviceList.size(); n++) {
            FixTank fixTank = serviceList.get(n);
            FCRValues.rightWindowUI[n].leftText = "对 " + fixTank.text + " 使用";
        }
        FCRValues.rightWindowUI[0].leftText = useItem.getDepict();
        FCRValues.rightWindowUI[5].rightText = "退出";
    }


    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if (entity.leftText.length() > 1) {
            if (i == 1) {
                //转给
            } else if (i == 2) {
                //丢弃
                if (isTankItem) {
                    useObject.tank.insertItemToKnapsack(useObject.witchKnapsack, "null");
                    useObject.tank.reInitShuxing();
                } else {
                    useObject.playerInfo.insertItemToKnapsack(useObject.witchKnapsack, "null");
                }
                fcv.setOpenUseItem(false);
            } else if (i == 3) {
                witchUseItem = serviceList.get(0);
                if (witchUseItem != null) {
                    //对前方使用
                    int cx = player1.getPlayer1CoorX();
                    int cy = player1.getPlayer1CoorY();
                    if (player1.getActionDir() == 0) {
                        cx += 1;
                    } else if (player1.getActionDir() == 3) {
                        cx -= 1;
                    } else if (player1.getActionDir() == 1) {
                        cy -= 1;
                    } else if (player1.getActionDir() == 2) {
                        cy += 1;
                    }
                    AItemUse aItemUse = FCItemUseCenter.getIn().findItemUseMethod(useObject.playerInfo, useObject.tank,
                            useItem.getName(), useObject.witchKnapsack, useObject.isTankBag);
                    ItemUseResult result = aItemUse.useInMapTarget(cx, cy);
                    if (fcv.isOpenUseItem()) {
                        witchUseItem = null;
                        resetUI();
                        FCRValues.rightWindowUI[0].leftText = result.resultString.get(0);
                    }
                }
            } else if (i == 4) {
                witchUseItem = serviceList.get(0);
                useForIt(true);
            }
        }
    }


    private void useForIt(boolean diret) {
        if (witchUseItem != null) {
            AItemUse aItemUse = FCItemUseCenter.getIn().findItemUseMethod(useObject.playerInfo, useObject.tank,
                    useItem.getName(), useObject.witchKnapsack, useObject.isTankBag);
            ItemUseResult result = null;
            if (!diret) {
                if (witchUseItem.playerInfo != null) {
                    result = aItemUse.useForPlayerTarget(witchUseItem.playerInfo);
                } else if (witchUseItem.tank != null) {
                    result = aItemUse.useForTankTarget(witchUseItem.tank);
                } else {
                    result = aItemUse.directUse();
                }
            } else {
                result = aItemUse.directUse();
            }
            if (fcv.isOpenUseItem()) {
                witchUseItem = null;
                resetUI();
                FCRValues.rightWindowUI[0].leftText = result.resultString.get(0);
            }
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        if (i == 5) {
            fcv.setOpenUseItem(false);
        } else {
            if (entity.leftText.length() > 1) {
                witchUseItem = serviceList.get(i);
                useForIt(false);
            }
        }
    }
}
